﻿var ns = namespace("kaskelot.sp.ui");

ns.adminkurs = function () {

    function getList() {
        $.ajax({
            url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientKursService.svc/GetKurser",
            type: "GET",
            data: {},
            processData: true,
            cache: false,
            success:
                function (data) {
                    if (!data.GetKurserResult.HasError) {
                        $("#kurs-results tbody").html(
							$("#kurs-template").render(data.GetKurserResult.Data)
						);
                    }
                },
            error:
                function (xhr) {
                    kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
                }
        });
    }

    function showCreateForm() {
        var selector = "#kurs-form";
        kaskelot.sp.ui.formutils.getAndShowForm({
            params: {
                formName: "KursForm",
                idString: "",
                parentIdString: ""
            },
            selector: selector,
            openFunction: function () {
                $("#kurs-form").dialog({ title: "Skapa kurs" });
            },
            okFunction: function () {
                $(selector).parent().block();

                var prog = kaskelot.sp.ui.formutils.getObject(selector);
                var data = "{\"form\":" + JSON.stringify(prog) + "}";

                var method = "CreateKurs";

                $.ajax({
                    url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientKursService.svc/" + method,
                    data: data,
                    type: "POST",
                    processData: false,
                    contentType: "application/json",
                    dataType: "json",
                    success: function (resp) {
                        if (resp[method + "Result"].HasError) {
                            kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
                        }
                        else {
                            $(selector).dialog("close");
                            getList();
                        }
                    },
                    error: function (xhr) {
                        kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
                    }
                });
            }
        });
    }

    function showEditForm(id) {
        var selector = "#kurs-form";
        kaskelot.sp.ui.formutils.getAndShowForm({
            params: {
                formName: "KursForm",
                idString: id,
                parentIdString: ""
            },
            selector: selector,
            openFunction: function () {
                $(selector).dialog({ title: "Redigera kurs" });
            },
            okFunction: function () {
                $(selector).parent().block();

                var prog = kaskelot.sp.ui.formutils.getObject(selector);
                var data = "{\"form\":" + JSON.stringify(prog) + "}";

                var method = "UpdateKurs";

                $.ajax({
                    url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientKursService.svc/" + method,
                    data: data,
                    type: "POST",
                    processData: false,
                    contentType: "application/json",
                    dataType: "json",
                    success: function (resp) {
                        if (resp[method + "Result"].HasError) {
                            kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
                        }
                        else {
                            $(selector).dialog("close");
                            getList();
                        }
                    },
                    error: function (xhr) {
                        kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
                    }
                });
            }
        });
    }

    function showDeleteForm(id) {
        var selector = "#kurs-form";
        kaskelot.sp.ui.formutils.getAndShowForm({
            params: {
                formName: "KursForm",
                idString: id,
                parentIdString: ""
            },
            selector: selector,
            openFunction: function () {
                $("#kurs-form").dialog({ title: "Ta bort kurs" });
                $("#kurs-form #Beskrivning").attr("disabled", "disabled");
                $("#kurs-form #PersonnummerTextbox").attr("disabled", "disabled");
                $("#kurs-form #NamnTextbox").attr("disabled", "disabled");
                $("#kurs-form #KursInfo").attr("disabled", "disabled");
            },
            okFunction: function () {
                $(selector).parent().block();

                var prog = kaskelot.sp.ui.formutils.getObject(selector);
                var data = "{\"form\":" + JSON.stringify(prog) + "}";

                var method = "DeleteKurs";

                $.ajax({
                    url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientKursService.svc/" + method,
                    data: data,
                    type: "POST",
                    processData: false,
                    contentType: "application/json",
                    dataType: "json",
                    success: function (resp) {
                        if (resp[method + "Result"].HasError) {
                            kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
                        }
                        else {
                            $(selector).dialog("close");
                            getList();
                        }
                    },
                    error: function (xhr) {
                        kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
                    }
                });
            }
        });
    }

    function init() {
        $("#kurs-form #kursansvarig-search-button").click(function () {
            searchPerson();
        });

        $("#kursansvarig-toggle-search").click(function () {
            $(this).hide();
            $("#kursansvarig-search-input-container").show();
        });
    }

    function searchPerson() {
    	var inaktiva = true;
    	var personnummer = $("#PersonnummerTextbox").val();
    	var postnummer = "";
    	var namn = $("#NamnTextbox").val();
    	var funktion = "";
    	var fritext = "";

    	$("#kurs-form").block({ message: '<h1>Var god vänta...</h1>' });

    	$.ajax({
    		url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientPersonService.svc/SearchPersoner",
    		type: "GET",
    		data: { namn: namn, personnummer: personnummer, postnummer: postnummer, fritext: fritext, funktionId: funktion, visaInaktiva: inaktiva },
    		processData: true,
    		cache: false,
    		success:
				function (data) {
					if (!data.SearchPersonerResult.HasError) {
						$("#kursansvarig-search-results tbody").html(
							$("#kursansvarig-template").render(data.SearchPersonerResult.Data)
						);
					}
					$("#kurs-form").unblock();
				},
    		error:
				function (xhr) {
					$("#kurs-form").unblock();
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
    	});
    }

    function selectPerson(id) {
    	$("#kurs-form tr").removeClass("selected-row");
    	$("#kurs-form #" + id).addClass("selected-row");
    	$("#Kursansvarig").val(id);
    }

    return {
        getList: getList,
        showEditForm: showEditForm,
        showCreateForm: showCreateForm,
        showDeleteForm: showDeleteForm,
        init: init,
        selectPerson: selectPerson
    };
}();